package com.sunnada.weixin.util.weixinPay;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

public class StrUtil {
	
	public static String getNotNullStrValue(Object obj) {
		if (obj == null) {
			return "";
		} else {
			return getNotNullStrValue(obj.toString());
		}
	}
	
	public static String getNotNullStrValue(String str) {
		if (str == null) {
			return "";
		} else {
			return getNotNullStrValue(str, "");
		}
	}

	public static String getNotNullStrValue(String str, String defStr) {
		if (StringUtils.isNotEmpty(str) && !str.trim().equals(""))
			return str.trim();
		return defStr;
	}
	
	public static int getNotNullIntValue(String str) {
		int intNum = 0;
		try {
			intNum = Integer.parseInt(str);
		} catch (NumberFormatException numForExc) {
			intNum = 0;
		}
		return intNum;
	}

	public static int getNotNullIntValue(String str, int defValue) {
		int intNum = getNotNullIntValue(str);
		if (intNum == 0)
			intNum = defValue;
		return intNum;
	}
	
	public static String obj2Str(Object object) {
		return ReflectionToStringBuilder.toString(object);
	}
	
	/**
     * 返回不空的double数据
     * @param str
     * @return
     */
    public static double getNotNullDoubleValue(String str) {
        double intNum = 0;
        try {
            intNum = Double.parseDouble(str);
        } catch (NumberFormatException numForExc) {
            intNum = 0;
        }
        return intNum;
    }
    public static double getNotNullDoubleValue(Object str){
        return getNotNullFloatValue(getNotNullStrValue(str));
    }
	
    public static float getNotNullFloatValue(String str) {
        float intNum = 0;
        try {
            intNum = Float.parseFloat(str);
        } catch (NumberFormatException numForExc) {
            intNum = 0;
        }
        return intNum;
    }
    
    /*
     * 判断对象是否为空，非空返回false； 空或NULL返回true
     */
    public static boolean isNull(Object obj) {
        if ( obj == null || "".equals(obj) || "null".equalsIgnoreCase(String.valueOf(obj))) {
            return true;
        } else {
            return false;
        }
    }
    
    public static boolean isNotNull(Object obj) {
        if ( obj == null || "".equals(obj) || "null".equalsIgnoreCase(String.valueOf(obj))) {
            return false;
        } else {
            return true;
        }
    }
    
    /**
	 * 如果为null返回默认值
	 * 
	 * @param obj
	 * @param def
	 * @return
	 */
	public static String get(Object obj, String... def) {
		if (obj != null) {
			return obj.toString();
		}
		for (String str : def) {
			if (str != null) {
				return str;
			}
		}
		return null;
	}
}
